package cn.nami.web.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.logging.Logger;

import cn.nami.web.frontpage.entity.User;

public class UserUtil
{
	private UserUtil(){}
	
	private static final Logger logger = Logger.getLogger(UserUtil.class.getName());
	private static final String CONFIG_FILE = "config.properties";

	public static String ADMIN_USER;
	public static String ADMIN_PWD;

	/**
	 * check whether the user is an admin user
	 * @param user
	 */
	public static boolean isAdmin(User user)
	{
		if(user == null)
		{
			return false;
		}
		return (ADMIN_USER.equals(user.getUsername()) && ADMIN_PWD.equals(user.getPassword()));
	}
	
	public static boolean isAdmin(String username, String password)
	{
		if(StringUtil.isEmpty(username) || StringUtil.isEmpty(password))
		{
			return false;
		}
		User user = new User();
		user.setUsername(username);
		user.setPassword(password);
		return isAdmin(user);
	}
	
	/**
	 * load the config files
	 */
	static
	{
		try
		{
			InputStream in = UserUtil.class.getResourceAsStream("/" + CONFIG_FILE);
			Properties prop = new Properties();
			prop.load(in);
			ADMIN_USER = prop.getProperty("ADMIN_USER");
			ADMIN_PWD = prop.getProperty("ADMIN_PWD");
			logger.info("ADMIN_USER : " + ADMIN_USER);
			logger.info("ADMIN_PWD : " + ADMIN_PWD);
			in.close();
		}
		catch (IOException e)
		{
			e.printStackTrace();
			logger.severe("Load config file error, " + e.getMessage());
		}
	}
	
	public static void main(String[] args)
	{
		System.out.println("Hello");
	}
}